package talk.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import talk.Bean.MyUser;
import talk.Data.Constant;
import talk.Net.HttpRequest;

import com.talk.debby.talk.R;

import talk.activity.HomeActivity;
import talk.dapter.AdapterUserList;
import talk.listener.OnItemClick;
import talk.rongyun.ChatUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.BaseFragment;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imlib.model.Conversation;

/**
 * Created by debby on 2018/4/8.
 * 描述:好友列表
 */

public class FriendListFragment extends BaseFragment implements View.OnClickListener {

    private HomeActivity activity;
    private AdapterUserList adapterUserList;
    private SwipeRefreshLayout refresh;
    private List<MyUser.FlistBean> flistBeans;

    @SuppressLint("StaticFieldLeak")
    public static FriendListFragment instance = null;//单例模式

    public static FriendListFragment getInstance() {
        if (instance == null) {
            instance = new FriendListFragment();
        }
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_friend_list, container, false);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {
        super.initView();
        activity = (HomeActivity) getActivity();
        initUserList();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }
    }

    /**
     * 打开聊天列表
     */
    private void openList() {
        Map<String, Boolean> supportedConversation = new HashMap<>();
        supportedConversation.put(Conversation.ConversationType.PRIVATE.getName(), false);
        supportedConversation.put(Conversation.ConversationType.CHATROOM.getName(), false);
        ChatUtil.startConversationList(activity, supportedConversation);
    }

    /**
     * 私聊
     *
     * @param userId 用户id
     */
    private void startPrivateChat(String userId, String name) {
        ChatUtil.startPrivateChat(activity, userId, name);
    }

    private void initUserList() {
        RecyclerView rv_user_list = rootView.findViewById(R.id.rv_user_list);
        //刷新
        refresh = rootView.findViewById(R.id.sr_refresh);
        refresh.setColorSchemeResources(R.color.blue, R.color.colorPrimary, R.color.colorPrimaryDark);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                searchUser(activity.getUser().getSinfo().getUid());
            }
        });
        // 设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_user_list.setLayoutManager(layoutManager);

        flistBeans = activity.getUser().getFlist();
        ///设置适配器
        adapterUserList = new AdapterUserList(activity, flistBeans, new OnItemClick() {
            @Override
            public void onClick(String id, String name) {
                startPrivateChat(id, name);
            }
        });
        rv_user_list.setAdapter(adapterUserList);
    }

    //
    private void searchUser(String id) {
        HttpRequest.findUser(id, new Observer<MyUser>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MyUser myUser) {
                flistBeans.clear();
                flistBeans.addAll(myUser.getFlist());
                adapterUserList.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refresh.setRefreshing(false);
            }

            @Override
            public void onComplete() {
                refresh.setRefreshing(false);
            }
        });
    }


}
